package demo2;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-15
 * Time: 13:23
 */
class Student{
    public int id;
    public String name;
    public Student(int id,String name){
        this.id = id;
        this.name = name;
    }
    //应对哈希冲突
    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return id == student.id;
    }
    //找到下标
    @Override
    public int hashCode() {
        return Objects.hashCode(id);
    }
}

public class MyHash<k,v> {
    //内部类
    public static class Node<k,v>{
        public k key;
        public v value;
        public Node<k,v> next;
        public Node(k key,v value){
            this.key = key;
            this.value = value;
        }
    }
    //
    public int useSize = 0;
    public Node<k,v>[] array;
    //构造方法
    public MyHash(){
        this.array = (Node<k, v>[]) new Node[10];
    }
    //
    public void add(k key,v value){
        int hash = key.hashCode();
        int index = hash % array.length;
        Node<k,v> cur = array[index];
        while (cur.next != null){
            if (cur.value.equals(value)){
                cur.value = value;
                return;
            }
            cur = cur.next;
        }
        cur.next = new Node<>(key,value);
        useSize++;
    }
}
